/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * leafget
 * Copyright (C) xinliGG 2008 <xinliGG@gmail.com>
 * 
 * leafget is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * leafget is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _NET_H_
#define _NET_H_

#include <glibmm/thread.h>

#include <string>
/*
#include "socket.h"
#include "http.h"
#include "ftp.h"
#include "mainwindow.h"
 */
#include "leafget.h"


class Http;
class Ftp;

class main_window;

 
class Net
{
public:

	Net ( main_window *, LEAF_TASK_DATA * );
	virtual ~Net ( void );

		/* initial functions */

	const bool create        ( const std::string & );

		/* set functions */

	const bool set_threadID   ( const int & );
	const bool set_id         ( const int & );
	const bool set_path       ( const std::string & );
	const bool set_name       ( const std::string & );
	const bool set_speed      ( const int & );
	const bool set_time       ( const std::string & );
	const bool set_comSize    ( const int & );
	const bool set_totalSize  ( const int & );
	const bool set_listPath   ( const std::string & );
	const bool set_enqueued   ( const bool & );

	const bool set_speedLimit ( const int & );

	const bool set_replace    ( const bool & );

		/* get functions */

	const int &         get_threadID   ( void ) const;
	const int &         get_id         ( void ) const;
	const std::string & get_name       ( void ) const;
	const int &         get_totalSize  ( void ) const;
	const int &         get_comSize    ( void ) const;
	const int &         get_percentage ( void ) const;
	const int &         get_speed      ( void ) const;
	const std::string & get_time       ( void ) const;
	const std::string & get_url        ( void ) const;
	const std::string & get_path       ( void ) const;
	const std::string & get_listPath   ( void ) const;

	const int           get_replace    ( void ) const;

		/* check functions */

	const bool isHTTP      ( void ) const;
	const bool isFTP       ( void ) const;
	const bool isSupported ( void ) const;
	const bool isCompleted ( void ) const;
	const bool isRunning   ( void ) const;

		/* action function */

	const bool define_info   ( const bool & );
	const bool start         ( void );
	const bool stop          ( void );

		/* signal function */

	sigc::signal< void, Net * > & signal_paused    ( void );
	sigc::signal< void, Net * > & signal_completed ( void );
	sigc::signal< void, Net * > & signal_failed    ( void );

private:

		/* set functions */
	
	const bool set_totalSize ( const std::string & );
	const bool set_url       ( const std::string & );
	const bool set_port      ( const std::string & );
	const bool set_address   ( const std::string & );
	const bool set_fileName  ( const std::string & );

		/* check function */

	const bool check_url       ( const std::string & ) const;
	const bool check_http_head ( const std::string & );

		/* error function */

	const int  show_error      ( const std::string &, const std::string &, const bool & ) const;

		/* action function */

	const bool download_file ( void );

		/* thread function */

	void   thread_function   ( void );


		/* socket */

	Http *httpSocket;
	Ftp  *ftpSocket;

		/* protocol: 0 -> HTTP |  1 -> FTP | 2 -> invalid */

//	int protocol;
	PROTOCOL protocol;
		/* file size in disc */

	long size;

		/* thread id */

	int threadID;

		/* speed limit */

	int speedLimit;

		/* boolean options */

	bool replace;
	bool running;
	bool donwload;
	bool changeURL;

		/* socket information */

	unsigned int port;

	std::string address;
	std::string fileName;

		/* local information */

	std::string path;

		/* GUI information */

	LEAF_TASK_DATA *leafget_download;

		/* window parent */

	main_window *win;

		/* thread */

	Glib::Thread *thread;

		/* signals */

	sigc::signal< void, Net * > leafget_signal_paused;
	sigc::signal< void, Net * > leafget_signal_completed;
	sigc::signal< void, Net * > leafget_signal_failed;

};

#endif // _NET_H_
